package org.testmyapp.tasks;

import java.util.HashMap;


import java.util.Map;
import java.util.Map.Entry;

import org.testmyapp.engine.IEngineServices;
import org.testmyapp.entities.Access;
import org.testmyapp.persistence.BusinessObjectPersistenceProvider;


//EXAMPLE!!!!
public class UserTaskOne implements TaskWorker {

	public static String access = "acessedBy";

	// EXAMPLE!!!!
	public String validate(IEngineServices aes,
			Map<String, String[]> parameterMap, String processInstanceId,
			BusinessObjectPersistenceProvider bopp) {
		try {
			for (Entry<String, String[]> param : parameterMap.entrySet()) {
				if (param.getKey().equals(access)) {
					String name = param.getValue()[0];
					Access access = new Access();
					access.setName(name);
					access.setProcessInstanceId(processInstanceId);
					bopp.persist(access);
					String sql = "select a from " + Access.tableName
							+ " a where a.processInstanceId = :pid";
					Map<String, Object> params = new HashMap<String, Object>();
					params.put("pid", processInstanceId);
					access = bopp.querySingle(sql, params, Access.class);
					addAsProcessVariable(aes, processInstanceId, access.getId()
							+ ":" + processInstanceId);
				}
			}
			return "";
		} catch (Exception e) {
			return "";
		}
	}

	private void addAsProcessVariable(IEngineServices aes,
			String processInstanceId, String value) {
		aes.addBusinessRelevantProcessVarToProcess(processInstanceId, value);
	}

}
